<template>
  <div class="report-form-content">
    <el-form class="my-form"
      ref="objForm"
      :model="obj"
      :rules="rules"
      label-position="right"
      label-width="auto"
      label-suffix="："
      :disabled="oper === 'view' || oper === 'supplement'"
    >
      <div class="tabs-form-head">
        <span>补充协议</span>
        <div class="">
          <el-button type="primary" plain @click="returnBtn">返回</el-button>
        </div>
      </div>
      <el-divider border-style="dashed" />
      <el-row :gutter="20">
        <el-col :sm="24" :lg="24" style="padding-left: 20px"  v-if="objList.length > 0">
          <el-card class="update-log">
            <div class="report-form-list-content">
              <div class="report-list">
                <span>补充协议经办人：</span>
                <span>{{ showIdLabel(userList, obj.supplementalUserId, 'name') }}</span>
              </div>
              <div class="report-list">
                <span>补充协议经办部门：</span>
                <span>{{ showIdLabel(organizationList, obj.supplementalDepartmentId, 'organizationName') }}</span>
              </div>
              <div class="report-list">
                <span>补充协议编号：</span>
                <span>{{ obj.supplementalContractNo }}</span>
              </div>
              <div class="report-list">
                <span>补充协议名称：</span>
                <span>{{ obj.supplementalContractName }}</span>
              </div>
              <div class="report-list">
                <span>补充协议创建时间：</span>
                <span>{{ obj.createTime }}</span>
              </div>
              <div class="report-list">
                <span>补充协议增加金额：</span>
                <span>{{ obj.supplementalAmount }}</span>
              </div>
            </div>
            <div class="report-form-list-content-1">
              <div class="report-list">
                <span>补签原因：</span>
                <span>{{ obj.supplementalReason }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :sm="24" :lg="24" style="padding-left: 20px" v-else>
          <el-card class="update-log">
            <el-empty :image-size="100" />
          </el-card>
        </el-col>
      </el-row>
      <div class="tabs-list-head" v-if="objList.length > 0">
        <span>补充协议附件</span>
      </div>
      <el-row :gutter="20" v-if="objList.length > 0">
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log">
              <el-table :data="oFiles"
                style="width: 100%; margin-top: 20px"
                row-key="id"
                border
                default-expand-all
                @selection-change="handleSelectionChange"
              >
                <el-table-column type="selection" width="55" />
                <el-table-column label="附件类型">
                  <template v-slot="{row}">
                    {{ showIdLabel(organizationList, row.attachmentType, 'organizationName') }}
                  </template>
                </el-table-column>
                <el-table-column prop="attachmentName" label="附件名称" />
                <el-table-column align="center" label="操作" width="160px" fixed="right">
                  <template #default="scope">
                    <el-button type="primary" link @click="downloadFile1(scope.row)">下载</el-button>
                    <el-button type="primary" link @click="viewFile1(scope.row)">预览</el-button>
                  </template>
                </el-table-column>
              </el-table>
            <!-- </el-form> -->
          </el-card>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="objList.length > 0">
        <div class="tabs-list-head">
          <span>协议合同信息</span>
        </div>
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log">
            <div class="report-form-list-content">
              <div class="report-list">
                <span>合同模版：</span>
                <span>{{ obj.contractTemplateId }}</span>
              </div>
            </div>
            <div class="report-form-list-content-1">
              <div class="report-list">
                <span>合同内容：</span>
                <span v-html="obj.supplementalContent"></span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-form>
    <el-dialog v-model="attachmentVisible" title="附件预览" width="70%" top="2vh">
      <attachmentPreview v-model:attachmentId="previewAttachmentId" />
      <div style="text-align:right;">
        <el-button type="danger" @click="attachmentVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import {ref, getCurrentInstance} from 'vue'
import {doGet, doDownload } from "@/config/http";
import {svc, showIdLabel, getListPageSize} from "@/config/common";

import attachmentPreview from '@/views/components/base/attachmentPreview.vue'
import { useRoute, useRouter } from 'vue-router'
import initListStore from '@/store/modules/initList'

const router = useRouter()
const route = useRoute()

const { proxy: ctx } = getCurrentInstance();

let obj = ref({
  contractInitialId: '', // 合同ID
  createBy: '', // 创建人用户ID
  createTime: '', // 	创建时间
  content: '',
  id: '', // 主键ID
  supplementalAmount: '', // 补充协议增加金额
  supplementalContent: '', // 补充协议内容
  supplementalContractName: '', // 补充协议名称
  supplementalContractNo: '', // 补充协议编号
  supplementalDepartmentId: '', // 	补充协议经办部门
  supplementalReason: '', // 	补签原因
  supplementalUserId: '', 	// 补充协议经办人
  updateBy: '', // 	最后修改人用户ID
  updateTime: '', // 最后修改时间
  version: '', // 数据版本
})

let oFiles = ref([])
let objList = ref([])
let attachmentVisible = ref(false)
let previewAttachmentId = ref(null)

// let contractTypeList = ref([]) // 合同类型
// let contractCooperationList = ref([]) // 往来单位
// let contractFeetypeList = ref([]) // 合同费用类型
// let contractTemplateList = ref([]) // 合同模版
// let bankList = ref([]) // 银行
let userList = initListStore().userList // 用户
// let governmentList = ref([]) //  政府单位
let organizationList = initListStore().organizationList // 部门
// let contractSignerList = ref([]) // 签订人
// let contractFiletypeList = ref([]) // 合同附件类型
// const initList = async () => {
//   let p1 = doGet(svc, '/contract-type/page', {pageSize: getListPageSize()}, false) // 合同类型
//   let p2 = doGet(svc, '/contract-cooperation/page', {pageSize: getListPageSize()}, false) // 往来单位
//   let p3 = doGet(svc, '/contract-feetype/page', {pageSize: getListPageSize()}, false) // 合同费用类型
//   let p4 = doGet(svc, '/contract-template/page', {pageSize: getListPageSize()}, false) // 合同模版
//   let p5 = doGet(svc, '/bank/page', {pageSize: getListPageSize()}, false) // 银行
//   let p6 = doGet(svc, '/user/page', {pageSize: getListPageSize()}, false) // 用户
//   let p7 = doGet(svc, '/government/page', {pageSize: getListPageSize()}, false) //  政府单位
//   let p8 = doGet(svc, '/organization/page', {pageSize: getListPageSize()}, false) // 部门
//   let p9 = doGet(svc, '/contract-signer/page', {pageSize: getListPageSize()}, false) // 签订人
//   let p10 = doGet(svc, '/contract-filetype/page', {pageSize: getListPageSize()}, false) // 合同附件类型

//   let resArray = await Promise.all([p1, p2, p3, p4, p5, p6, p7, p8, p9, p10])
//   contractTypeList.value = resArray[0]['records']
//   contractCooperationList.value = resArray[1]['records']
//   contractFeetypeList.value = resArray[2]['records']
//   contractTemplateList.value = resArray[3]['records']
//   bankList.value = resArray[4]['records']
//   userList.value = resArray[5]['records']
//   governmentList.value = resArray[6]['records']
//   organizationList.value = resArray[7]['records']
//   contractSignerList.value = resArray[8]['records']
//   contractFiletypeList.value = resArray[9]['records']
// }

const initObj = async () =>  {
  if (route.query.id) {
    objList.value = await doGet(svc, '/contract-initial-supplemental/listByField', {field: 'contractInitialId', values: route.query.id}, true)
    console.log('objList-----------',objList.value)
    if (objList.value.length > 0) {
      obj.value = objList.value[0]
    }
    oFiles.value = await doGet(svc, '/contract-initial-supplemental-attachment/listByField', {field: 'contractInitialSupplementalId', values: obj.value.id}, false)
  }
}

// initList()
initObj()

const downloadFile1 = (row) => {
  console.log('row',row)
  if (row.attachmentId) {
    doDownload(svc, `/customAttachment/download/${row.attachmentId}`, {})
  } else {
    doDownload(svc, `/customAttachment/download/${row.id}`, {})
  }
}
const viewFile1 = (row) => {
  attachmentVisible.value = true
  setTimeout(() => {
    if (row.attachmentId) {
      previewAttachmentId.value = row.attachmentId
    } else {
      previewAttachmentId.value = row.id
    }
    console.debug('previewAttachmentId.value',previewAttachmentId.value)
  },100)
}

const returnBtn = () => {
  switch (route.query.kind) {
    case '1':
      ctx.goTargetByName('contractInitial', {});
      break;
    case '2':
      ctx.goTargetByName('contractMain', {})
      break;
  }
}

</script>
<style scoped>
.report-form-content {
  padding: 20px;
}
.tabs-list-head {
  display: flex;
  align-items: center;
}
.tabs-list-head span {
  font-size: 22px !important;
  color: #1ac4ad !important;
  font-weight: bold;
}
.update-log {
  width: 100%;
  margin: 30px 0;
}
.report-list {
  width: 31%;
  margin: 15px;
  display: flex;
  align-items: center;
}
.report-list span:nth-child(1) {
  display: inline-block;
  width: 150px;
  text-align: right;
}
.tabs-form-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tabs-form-head span {
  font-size: 18px;
  color: #303133;
  font-weight: bold;
}
</style>
